axios.defaults.baseURL = 'http://api-breakingnews-web.itheima.net'
//请求拦截器
axios.interceptors.request.use(
  function (config) {
    if (config.url.startsWith('/my')) {
      config.headers.Authorization = sessionStorage.getItem('big-event')
    }
    return config
  },
  function (error) {
    // 对请求错误做些什么
    return Promise.reject(error)
  }
)
//响应拦截器
axios.interceptors.response.use(
  function (response) {
    console.log('响应拦截器执行了', response)
    const {
      data: { message, status },
    } = response
    if (status === 0) {
      layer.msg(message)
    }
    if (status === 1) {
      location.href = './login.html'
    }
    return response
  },
  function (error) {
    return Promise.reject(error)
  }
)

//封装注册请求接口
const postRegister = function (obj) {
  return axios({
    method: 'post',
    url: '/api/reguser',
    data: objRegister(obj),
  })
}
//封装登录请求接口
const postLogin = function (obj) {
  return axios({
    method: 'post',
    url: '/api/login',
    data: objRegister(obj),
  })
}
//封装获取个人信息请求接口
const GetUserInfo = function () {
  return axios({
    method: 'get',
    url: '/my/userinfo',
  })
}
//修改用户信息接口
const postReviseInfo = function (obj) {
  return axios({
    method: 'post',
    url: '/my/userinfo',
    data: objRegister(obj),
  })
}
//重置密码请求接口
const postResetPwd = function (obj) {
  return axios({
    method: 'post',
    url: '/my/updatepwd',
    data: objRegister(obj),
  })
}
//上传头像请求接口
const PostUploadAvatar = function (obj) {
  return axios({
    method: 'post',
    url: '/my/update/avatar',
    data: objRegister(obj),
  })
}
//获取文章列表请求
const GetArticleList = function () {
  return axios({
    method: 'get',
    url: '/my/article/cates',
  })
}
//添加文章类别
const PostAddCategory = function (obj) {
  return axios({
    method: 'post',
    url: '/my/article/addcates',
    data: objRegister(obj),
  })
}
//删除文章分类
const GetDeleteClassify = function (id) {
  return axios({
    method: 'get',
    url: `/my/article/deletecate/${id}`,
  })
}
//修改文章类别和名称
const PostReviseClassify = function (obj) {
  return axios({
    method: 'post',
    url: '/my/article/updatecate',
    data: objRegister(obj),
  })
}
//发布新文章
const PostNewArticle = function (data) {
  return axios({
    method: 'post',
    url: '/my/article/add',
    data: data,
  })
}
//获取文章列表
const GetListWorks = function (data) {
  return axios({
    method: 'get',
    url: '/my/article/list',
    params: data,
  })
}
//
const GetBianJi = function (id) {
  return axios({
    method: 'get',
    url: `/my/article/delete/${id}`,
  })
}
